package com.zhiqianbao.share;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import com.zhiqianbao.application.MyApplication;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.io.ByteArrayOutputStream;

/**
 * Author     wildma
 * DATE       2017/07/16
 * Des	      ${友盟分享工具类}
 */
public class ShareUtils {

    public void shareQQ(Tencent _tencent,final Activity activity,String shareUrl,String shareTitle,
                       String shareContent,String shareImgUrl){
                final Bundle bundle = new Bundle();
                bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
                bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, shareUrl);
                bundle.putString(QQShare.SHARE_TO_QQ_TITLE, shareTitle);
                bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, shareContent);
                bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareImgUrl);
                bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "值钱包");
                _tencent.shareToQQ(activity, bundle, new IUiListener() {
                    @Override
                    public void onComplete(Object o) {
                        Toast.makeText(activity,  " 分享成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(UiError uiError) {
                        Toast.makeText(activity,  " 分享错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel() {
                        Toast.makeText(activity,  " 分享取消", Toast.LENGTH_SHORT).show();
                    }
                });
    }
    public void shareWxFriends(int shareType,String shareUrl,String shareTitle,
                               String shareContent,Bitmap bitmap){
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = shareUrl;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = shareTitle;
        msg.description = shareContent;
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
        bitmap.recycle();
        msg.thumbData = bitmap2Bytes(thumbBmp);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("webpage");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        MyApplication.mWxApi.sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
    public static final int IMAGE_SIZE=32768;//微信分享图片大小限制
    /**
     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
     * @param bitmap
     * @return
     */
    public static byte[] bitmap2Bytes(Bitmap bitmap) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        int options = 100;
        while (output.toByteArray().length > IMAGE_SIZE&& options != 10) {
            output.reset(); //清空output
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%，把压缩后的数据存放到output中
            options -= 10;
        }
        return output.toByteArray();
    }
}
